//
//  UIView+ACPToast.m
//  AutoCreatePropertyDemo
//
//  Created by anmin on 2017/11/24.
//  Copyright © 2017年 puhui. All rights reserved.
//

#import "UIView+ACPToast.h"

@interface NSString (ACPLabelSize)

- (CGFloat)ACP_labelHeightWithFont:(UIFont *)font labelWidth:(CGFloat)labelWidth;
- (CGFloat)ACP_labelWidthWithFont:(UIFont *)font labelHeight:(CGFloat)labelHeight;

@end

@implementation NSString (ACPLabelSize)

- (CGSize)ACP_labelSizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    CGSize size = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;
    return CGSizeMake(size.width + 1., size.height + 1.);
}

- (CGFloat)ACP_labelHeightWithFont:(UIFont *)font labelWidth:(CGFloat)labelWidth {
    CGFloat height = [self boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size.height;
    return height + 1;
}

- (CGFloat)ACP_labelWidthWithFont:(UIFont *)font labelHeight:(CGFloat)labelHeight {
    CGFloat width = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, labelHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size.width;
    return width + 1;
}
@end

@implementation UIView (ACPToast)

- (UIView *)toast_label_with_msg:(NSString *)msg {
    
    UIView *toast_view = [[UIView alloc] init];
    toast_view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
    
    CGFloat left_margin = 15.;
    CGFloat right_margin = 15.;
    CGFloat panding = 5.;
    
    CGRect screen_bound = self.bounds;
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    label.font = [UIFont systemFontOfSize:14.];
    label.backgroundColor = [UIColor clearColor];
    
    CGFloat label_max_length = CGRectGetWidth(screen_bound) - left_margin - right_margin - panding * 2;
    CGFloat label_max_height = 100.;
    
    CGSize label_size = [msg ACP_labelSizeWithFont:label.font maxSize:CGSizeMake(label_max_length, label_max_height)];
    
    label.frame = CGRectMake(panding, panding, label_size.width, label_size.height);
    label.text = msg;
    toast_view.bounds = CGRectMake(0, 0, label_size.width + 2 * panding, label_size.height + 2 * panding);
    [toast_view addSubview:label];
    toast_view.layer.masksToBounds = YES;
    toast_view.layer.cornerRadius = 4.;
    
    return toast_view;
}


- (void)showToast:(NSString *)string {
    UIView *toast_view = [self toast_label_with_msg:string];
    CGRect frame = toast_view.bounds;
    frame.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(frame)) / 2.;
    frame.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(frame)) / 2.;
    toast_view.frame = frame;
    [self addSubview:toast_view];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [toast_view removeFromSuperview];
    });
}

@end
